package com.ruoyi.legal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.legal.domain.WorkDetail;
import com.ruoyi.legal.service.IWorkDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.legal.mapper.WorkMapper;
import com.ruoyi.legal.domain.Work;
import com.ruoyi.legal.service.IWorkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工作日程Service业务层处理
 *
 * @author xwj
 * @date 2025-04-29
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements IWorkService {
    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private IWorkDetailService workDetailService;

    /**
     * 查询工作日程
     *
     * @param id 工作日程主键
     * @return 工作日程
     */
    @Override
    public Work selectWorkById(Long id) {
        Work work = workMapper.selectWorkById(id);
        work.setDetailList(workDetailService.selectWorkDetailListByWorkId(id));

        return work;
    }

    /**
     * 查询工作日程列表
     *
     * @param work 工作日程
     * @return 工作日程
     */
    @Override
    public List<Work> selectWorkList(Work work) {
        return workMapper.selectWorkList(work);
    }

    /**
     * 新增工作日程
     *
     * @param work 工作日程
     * @return 结果
     */
    @Override
    @Transactional
    public Work insertWork(Work work) {

        save(work);
        insertDetail(work);
        return work;
    }

    /**
     * 修改工作日程
     *
     * @param work 工作日程
     * @return 结果
     */
    @Override
    public Work updateWork(Work work) {

        //  workMapper.updateWork(work);
        insertDetail(work);
        updateById(work);
        return work;
    }

    private void insertDetail(Work work) {
        List<WorkDetail> details = work.getDetailList();
        Long id = work.getId();;
        Date now = new Date();

        if(StringUtils.isNotNull(details)) {
            for(WorkDetail detail: details) {
                detail.setWorkId(id);

                if(detail.getId() == null) {
                    detail.setCreateTime(now);
                    workDetailService.insertWorkDetail(detail);
                } else {
                    workDetailService.updateById(detail);
                }
            }
        }

        // 要删除的数据
        List<Long> deleteIds = work.getDeleteIds();
        if(deleteIds != null && deleteIds.size() > 0) {
            Long[] ids = deleteIds.toArray(new Long[0]);
            workDetailService.deleteWorkDetailByIds(ids);
        }

    }

    /**
     * 批量删除工作日程
     *
     * @param ids 需要删除的工作日程主键
     * @return 结果
     */
    @Override
    public int deleteWorkByIds(Long[] ids) {
        return workMapper.deleteWorkByIds(ids);
    }

    /**
     * 条件删除
     *
     * @param work 工作日程
     * @return
     */
    @Override
    public int delete(Work work) {
        QueryWrapper<Work> qw = new QueryWrapper<>();
        QueryTool.condition(qw, work);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     *
     * @param work 工作日程
     * @return
     */
    @Override
    public Pages<Work> queryPage(Work work) {
        QueryWrapper<Work> qw = new QueryWrapper<>();
        QueryTool.condition(qw, work);
        IPage<Work> ipage = new Query<Work>().getPage(work);

        IPage<Work> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    /**
     * 删除工作日程信息
     *
     * @param id 工作日程主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWorkById(Long id) {
        workDetailService.deleteWorkDetailByWorkId(id);
        return workMapper.deleteWorkById(id);
    }
}
